﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Decorate;
using S4Store.Service;
using S4Store.Domain.Report;
using S4Store.Models;
using S4Store.Models.Chart;

namespace S4Store.Controllers
{
    public class DecorateController : ReportBaseController
    {
        public ReportService ReportService { get; set; }
        public CarKpiService CarKpiService { get; set; }
        public CarService CarService { get; set; }
        public EmployeeService EmployeeService { get; set; }
        public SellerKpiService SellerKpiService { get; set; }

        public ActionResult CarIndex()
        {
            CarIndexModel model = new CarIndexModel();
            model.ChartData.Name = "各类别精品装潢KPI概况";
            model.ChartData.YAxis = "比率(%)";
            model.ChartData.XAxis = new List<string> { "套餐车型设计率", "套餐车销售率", "装潢车销售率" };
            using (var t = new Transtion())
            {
                t.Inject(ReportService);

                CarSaleReport report = ReportService.GetSaleReport(Month.Year, Month.Month, Store.Id);

                S4Store.Models.Chart.ChartSeries series1 = new S4Store.Models.Chart.ChartSeries();
                series1.Name = "目标";
                series1.Data.Add(report.CarKpiMonthConfig.PakeageDesignTarget);
                series1.Data.Add(report.CarKpiMonthConfig.PakeageSaleRateTagrt);
                series1.Data.Add(report.CarKpiMonthConfig.DecorateSaleRateTagrt);
                model.ChartData.Serieses.Add(series1);

                S4Store.Models.Chart.ChartSeries series2 = new S4Store.Models.Chart.ChartSeries();
                series2.Name = "实际";
                series2.Data.Add(report.PackageCarSJLV);
                series2.Data.Add(report.PackageXSLV);
                series2.Data.Add(report.DecorateXSLV);
                model.ChartData.Serieses.Add(series2);
                model.ChartData.Colors = new List<string> { S4Store.Models.Chart.Color.Green, S4Store.Models.Chart.Color.Blue };


                RedGreenIndex index1 = new RedGreenIndex();
                index1.Size = RedGreenIndex.IndexSize.Middle;
                if (report.PackageCarSJLV >= report.CarKpiMonthConfig.PakeageDesignTarget)
                {
                    index1.Type = RedGreenIndex.IndexType.Green;
                }
                else
                {
                    index1.Type = RedGreenIndex.IndexType.Red;
                }
                index1.Value = report.PackageCarSJDiff.ToString();
                index1.Tip =  "各车型套餐车版本更新状况KPI";
                index1.Title = "实际套餐车设计数与套餐车设计数目标的差值";
                index1.Value = report.PackageCarSJDiff.ToString()+"%";
                index1.Url = Url.Action("PackageCarVerionChage");
                model.RedGreenIndexs.Add(index1);



                RedGreenIndex index2 = new RedGreenIndex();
                index2.Size = RedGreenIndex.IndexSize.Middle;
                if (report.PackageXSLV >= report.CarKpiMonthConfig.PakeageSaleRateTagrt)
                {
                    index2.Type = RedGreenIndex.IndexType.Green;
                }
                else
                {
                    index2.Type = RedGreenIndex.IndexType.Red;
                }
                index2.Tip = "各车型套餐车销售状况KPI";
                index2.Title = "实际套餐车销售数与套餐车目标销售数的差值";
                index2.Value = report.PackageCountDiff.ToString();
                index2.Url = Url.Action("PackageCarSale");
                model.RedGreenIndexs.Add(index2);


                RedGreenIndex index3 = new RedGreenIndex();
                index3.Size = RedGreenIndex.IndexSize.Middle;
                if (report.DecorateXSLV >= report.CarKpiMonthConfig.DecorateSaleRateTagrt)
                {
                    index3.Type = RedGreenIndex.IndexType.Green;
                }
                else
                {
                    index3.Type = RedGreenIndex.IndexType.Red;
                }
                index3.Tip = "各车型装潢车销售状况KPI";
                index3.Title = "实际装潢车销售数与装潢车目标销售数的差值";
                index3.Value = report.DecorateCountDiff.ToString();
                index3.Url = Url.Action("DecorateSale");
                model.RedGreenIndexs.Add(index3);

                t.Commit();
            }



            this.InjectReportModel(model);
            model.BarModel.Action = "CarIndex";
            model.BarModel.Controller = "Decorate";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult PackageCarVerionChage()
        {
            PackageCarVerionChageModel model = new PackageCarVerionChageModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                var carbrands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in carbrands)
                {
                    var report = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PackageCarVerionChage";
            model.BarModel.Controller = "Decorate";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult PackageCarVerionChageDetail()
        {
            PackageCarVerionChageDetailModel model = new PackageCarVerionChageDetailModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var carmodel in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, carmodel.Id);
                    model.Reports.Add(report);
                }
                model.CarBrandReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PackageCarVerionChageDetail";
            model.BarModel.Controller = "Decorate";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a class='historyrecord edit' backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>精品装潢业绩动态>车型查询>套餐车型设计率" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li></ul>";
		}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult PackageCarSale()
        {
            PackageCarSaleModel model = new PackageCarSaleModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                var carbarnds= CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in carbarnds)
                {
                    var report = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "PackageCarSale";
            model.BarModel.Controller = "Decorate";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("PackageCarSaleHistory") + "' target='navtab' width='30%' title='各车型套餐车销售状况KPI历史数据查询'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult PackageCarSaleHistory()
        {
            PackageCarSaleHistoryModel model = new PackageCarSaleHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.CarBrands = CarKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    S4Store.Models.Decorate.PackageCarSaleHistoryModel.MonthCarBrandSaleReport monthreport = new S4Store.Models.Decorate.PackageCarSaleHistoryModel.MonthCarBrandSaleReport();
                    monthreport.Month = dt;
                    foreach (var brand in model.CarBrands)
                    {
                        var report = ReportService.GetCarBrandSaleReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        monthreport.Reports.Add(report);
                    }
                    model.Reports.Add(monthreport);
                }
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PackageCarSaleHistory";
            model.BarModel.Controller = "Decorate";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

        public ActionResult PackageCarSaleDetail()
        {
            PackageCarSaleDetailModel model = new PackageCarSaleDetailModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var carmodel in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, carmodel.Id);
                    model.Reports.Add(report);
                }
                model.CarBrandReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PackageCarSaleDetail";
            model.BarModel.Controller = "Decorate";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a class='historyrecord edit' backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>精品装潢业绩动态>车型查询>套餐车销售率" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li></ul>";
		}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult DecorateSale()
        {
            DecorateSaleModel model = new DecorateSaleModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                var carbarnds = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in carbarnds)
                {
                    var report = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, brand.Id);
                    model.Reports.Add(report);
                }
            }
            this.InjectReportModel(model);
            model.BarModel.Action = "DecorateSale";
            model.BarModel.Controller = "Decorate";
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  href='" + Url.Action("DecorateSaleHistory") + "' target='navtab' width='30%' title='各车型装潢车销售状况KPI历史数据'><span class='icon search'>历史记录查询</span></a></li></ul>";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }


        public ActionResult DecorateSaleHistory()
        {
            DecorateSaleHistoryModel model = new DecorateSaleHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.CarBrands = CarKpiService.GetCarbrandsForStoreMonths(Start.Value, End.Value, Store.Id).ToList();
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    S4Store.Models.Decorate.DecorateSaleHistoryModel.MonthCarBrandSaleReport monthreport = new S4Store.Models.Decorate.DecorateSaleHistoryModel.MonthCarBrandSaleReport();
                    monthreport.Month = dt;
                    foreach (var brand in model.CarBrands)
                    {
                        var report = ReportService.GetCarBrandSaleReport(dt.Year, dt.Month, Store.Id, brand.Id);
                        monthreport.Reports.Add(report);
                    }
                    monthreport.AllCarReport = ReportService.GetSaleReport(dt.Year, dt.Month, Store.Id);
                    model.Reports.Add(monthreport);
                }
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "DecorateSaleHistory";
            model.BarModel.Controller = "Decorate";
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult DecorateSaleDetail()
        {
            DecorateSaleDetailModel model = new DecorateSaleDetailModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(CarKpiService);
                t.Inject(CarService);
                model.CarBrand = CarService.GetCarBrandQuery().FirstOrDefault(x => x.Id == SearchCarBrandId.Value);
                var carmodels = CarKpiService.GetCarmodelsForStoreMonth(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value).ToList();
                foreach (var carmodel in carmodels)
                {
                    var report = ReportService.GetCarModelSaleReport(Month.Year, Month.Month, Store.Id, carmodel.Id);
                    model.Reports.Add(report);
                }
                model.CarBrandReport = ReportService.GetCarBrandSaleReport(Month.Year, Month.Month, Store.Id, SearchCarBrandId.Value);
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "DecorateSaleDetail";
            model.BarModel.Controller = "Decorate";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar'><li><a  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt()  ,referdesc="销售管理>精品装潢业绩动态>车型查询>装潢车销售率" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li></ul>";
		}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult Index()
        {
            IndexModel model = new IndexModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(EmployeeService);
                t.Inject(CarKpiService);

                var employees = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.FieldSaler).ToList();
                model.Employees = employees;
                foreach (var employee in employees) {
                    SellerPersonReport report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, employee.Id);
                    model.Reports.Add(report);
                }

                var telemployees = EmployeeService.GetEnableEmployee(Store.Id, Domain.EmployeeType.TelSaler).ToList();
                model.TelEmployees = telemployees;
                foreach (var employee in telemployees)
                {
                    SellerPersonReport report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, employee.Id);
                    model.TelSellerReports.Add(report);
                }
                model.SellerPerformanceStandard = ReportService.GetSellerPerformanceStandard(Month.Year, Month.Month, Store.Id);
                //model.CarKpiMonthConfig = CarKpiService.GetCarKpiMonthConfig(Month.Year, Month.Month, Store.Id, true);
                t.Commit();
            }



            this.InjectReportModel(model);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "Decorate";
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            return View(model);
        }

        public ActionResult PersonDetail()
        {
            PersonDetailModel model = new PersonDetailModel();
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(EmployeeService);
                t.Inject(CarKpiService);
                t.Inject(SellerKpiService);

                var employee = EmployeeService.GetQuery().FirstOrDefault(x=>x.Id==SearchSellerId.Value);
                model.Employee = employee;
                var carbrands = CarKpiService.GetCarbrandsForStoreMonth(Month.Year, Month.Month, Store.Id).ToList();
                foreach (var brand in carbrands)
                {
                    var report = ReportService.GetSellerPersonBrandReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value, brand.Id);
                    model.Reports.Add(report);
                }
                model.Report = ReportService.GetSellerPersonReport(Month.Year, Month.Month, Store.Id, SearchSellerId.Value);
                model.SellerPerformanceStandard = ReportService.GetSellerPerformanceStandard(Month.Year, Month.Month, Store.Id);
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PersonDetail";
            model.BarModel.Controller = "Decorate";
			if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.SaleTutors ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
            model.BarModel.PlusItem = "<ul class='toolBar' style='width:400px'><li><a class='historyrecord edit' backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.Sale.ToInt() ,referdesc="销售管理>精品装潢业绩动态>人员查询" }) + "' target='dialog' width='30%'><span class='icon edit'>填写改善指导建议</span></a></li><li><a  backtype='html' href='" + Url.Action("PersonHistory", "Decorate", new { SearchSellerId = SearchSellerId.Value }) + "' target='navtab' ><span class='icon edit'>" + model.Employee.Name + "装潢历史数据查询</span></a></li></ul>";
			}else
		{
			  model.BarModel.PlusItem = "<ul class='toolBar' style='width:400px'><li><a  backtype='html' href='" + Url.Action("PersonHistory", "Decorate", new { SearchSellerId = SearchSellerId.Value }) + "' target='navtab' ><span class='icon edit'>" + model.Employee.Name + "装潢历史数据查询</span></a></li></ul>";
			}
            model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }


        public ActionResult PersonHistory()
        {
            PersonHistoryModel model = new PersonHistoryModel();
            if (Start == null)
            {
                Start = DateTime.Now.AddMonths(-6);
                End = DateTime.Now;
            }
            using (var t = new Transtion())
            {
                t.Inject(ReportService);
                t.Inject(EmployeeService);
                model.Seller = EmployeeService.GetQuery().FirstOrDefault(x=>x.Id==SearchSellerId.Value);
                for (DateTime dt = Start.Value; dt <= End; dt = dt.AddMonths(1))
                {
                    var report = ReportService.GetSellerPersonReport(dt.Year, dt.Month, Store.Id, SearchSellerId.Value);
                    model.Reports.Add(report);
                }
                t.Commit();
            }

            this.InjectReportModel(model);
            model.BarModel.Action = "PersonHistory";
            model.BarModel.Controller = "Decorate";
            model.Start = Start.Value;
            model.End = End.Value;
            model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            model.BarModel.RemoveView(ReportBarModel.ViewItem.Stores);
            return View(model);
        }

    }
}
